﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MINERP.MODEL.Statistics;
using MINERP.MODEL.FoundationModel;
using FastReport;

namespace MINERP.Statistics
{
    public partial class FrmBill : Form
    {
        private List<MODEL.Statistics.Shop_Info> ListShop = new List<MINERP.MODEL.Statistics.Shop_Info>();    //商户集合 
        public List<Bill_Info> FilterBill { get; set; }                         //经过筛选的账单集合

        private BLL.Statistics.StatisticsBLL SSBLL = new MINERP.BLL.Statistics.StatisticsBLL();
        public FrmBill()
        {
            InitializeComponent();
        }
        private static FrmBill instance = null;
        public static FrmBill Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new FrmBill();
                }
                if (instance.IsDisposed)
                {
                    instance = new FrmBill();
                }
                return instance;
            }
        }

        private void FrmBill_Load(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;
            string nowStr = now.ToString("yyyy-MM-dd HH:mm");
            dtStartTime.Text = nowStr;
            dtEndTime.Text = nowStr;

            //获取商户集合
            GetShopList();
            BindShop();
        }

        #region 获取商户集合
        /// <summary>
        /// 获取商户集合
        /// </summary>
        private void GetShopList()
        {
            ListShop.Clear();
            ListShop = SSBLL.GetShopList();
        }
        #endregion

        #region 设置商户下拉列表
        /// <summary>
        /// 设置商户下拉列表
        /// </summary>
        private void BindShop()
        {
            if (cbShopID.Properties.Items != null)
                cbShopID.Properties.Items.Clear();
            foreach (MINERP.MODEL.Statistics.Shop_Info s in ListShop)
            {
                cbShopID.Properties.Items.Add(s.ShopName);
            }
        }
        #endregion

        private void btnFind_Click(object sender, EventArgs e)
        {
            try
            {
                if (cbShopID.Text == "--请选择--")
                {
                    return;
                }
                DateTime startTime = Convert.ToDateTime(dtStartTime.Text);
                DateTime endTime = Convert.ToDateTime(dtEndTime.Text);
                if (startTime >= endTime)
                    return;
                int shopId = 0;
                string shopName = cbShopID.Text;
                foreach (MINERP.MODEL.Statistics.Shop_Info s in ListShop)
                {
                    if (s.ShopName == shopName)
                    {
                        shopId = s.ShopID;
                        break;
                    }
                }
                if (shopId == 0)
                    return;
                List<Bill_Info> ListBill = SSBLL.GetBillInfo(shopId, startTime, endTime);
                FrmSetBill sb = new FrmSetBill();
                sb.ListBill = ListBill;
                sb.fb = this;
                sb.ShowDialog();
                if (FilterBill == null || FilterBill.Count == 0)
                    return;
                List<SHOP_Bill_Info> ListBillDetail = GetBillDetail(FilterBill);
                PrintBill(ListBillDetail);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #region 获取账单的详情
        /// <summary>
        /// 获取账单的详情
        /// </summary>
        /// <param name="bills"></param>
        /// <returns></returns>
        private List<SHOP_Bill_Info> GetBillDetail(List<MODEL.Statistics.Bill_Info> bills)
        {
            try
            {
                List<int> billIds = new List<int>();
                foreach (MODEL.Statistics.Bill_Info si in bills)
                {
                    billIds.Add(si.BillID);
                }
                List<SHOP_Bill_Info> ShopBillDetails = SSBLL.GetBillDetails(billIds);
                return ShopBillDetails;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #region helper methods
        /// <summary>
        /// 打印帐单
        /// </summary>
        /// <param name="sbinfo"></param>
        private void PrintBill(List<SHOP_Bill_Info> sbinfoList)
        {
            DataTable tableTitle = CreateShopBillTitle();
            DataTable tableDetail = CreateShopBillDetail();
            DataTable tableSubDetail = CreateShopBillSubDetail();

            foreach (SHOP_Bill_Info sbinfo in sbinfoList)
            {
                DataRow titleRow = tableTitle.NewRow();
                titleRow["ShopName"] = sbinfo.Shop.ShopName;
                titleRow["Tel1"] = sbinfo.Shop.Tel1;
                titleRow["ShopId"] = sbinfo.Shop.ShopId;
                titleRow["BillId"] = sbinfo.BillId;
                titleRow["BillOrderCode"] = sbinfo.BillOrderCode;
                titleRow["BillCreateTime"] = sbinfo.CreateTime;                
                tableTitle.Rows.Add(titleRow);




                List<Bill_Detail_Info> detailList = sbinfo.List_Detail.ToList();

                foreach (Bill_Detail_Info billDetail in detailList)
                {
                    DataRow row = tableDetail.NewRow();
                    row["BillId"] = sbinfo.BillId;
                    row["BillDetailId"] = billDetail.BillDetailId;
                    row["OrderInfoCode"] = billDetail.OrderInfoCode;
                    row["OrderProductName"] = billDetail.OrderProductName;
                    row["CtmName"] = billDetail.CtmName;
                    tableDetail.Rows.Add(row);
                    List<Bill_Detail_Detail_Info> subDetail = billDetail.ListDetail_Detail.ToList();
                    foreach (Bill_Detail_Detail_Info detail in subDetail)
                    {
                        DataRow rowSub = tableSubDetail.NewRow();
                        rowSub["DetailName"] = detail.DetailName;
                        rowSub["BillDetailId"] = billDetail.BillDetailId;
                        rowSub["Spec"] = detail.Spec;
                        rowSub["Num"] = detail.Num;
                        rowSub["UnitPrice"] = detail.UnitPrice;
                        rowSub["Count"] = detail.Count;
                        rowSub["Area"] = detail.Area;
                        tableSubDetail.Rows.Add(rowSub);
                    }
                }
            }
            tableTitle.TableName = "tableTitle";
            tableDetail.TableName = "tableDetail";
            tableSubDetail.TableName = "tableSubDetail";


            DataSet set = new DataSet();
            set.Tables.Add(tableTitle);
            set.Tables.Add(tableDetail);
            set.Tables.Add(tableSubDetail);

            set.WriteXml(@"C:\Windows\xmldata\PrintShopBill2.xml", XmlWriteMode.WriteSchema);

            //set.WriteXml(@"C:\Windows\xmldata\PrintShopBill.xml");
            //Report report = new Report();

            report.Preview = previewControl1;
            report.Load(Application.StartupPath + "\\PrintShopBill2.frx");
            report.RegisterData(set);
            previewControl1.Refresh();
            previewControl1.Show();
            report.Show();
            //previewControl1.Load(Application.StartupPath + @"\PrintShopBill.frx");
            //report.Show();

        }
        /// <summary>
        /// 创建title表
        /// </summary>
        /// <returns></returns>
        private DataTable CreateShopBillTitle()
        {
            DataTable table = new DataTable();
            table.Columns.Add("ShopName", typeof(string));
            table.Columns.Add("Tel1", typeof(string));
            table.Columns.Add("ShopId", typeof(int));
            table.Columns.Add("BillOrderCode", typeof(string));
            table.Columns.Add("BillId", typeof(int));
            table.Columns.Add("BillCreateTime", typeof(DateTime));
            return table;

        }
        /// <summary>
        /// 创建明细产品
        /// </summary>
        /// <returns></returns>
        private DataTable CreateShopBillDetail()
        {
            DataTable table = new DataTable();
            table.Columns.Add("BillId", typeof(int));
            table.Columns.Add("BillDetailId", typeof(int));
            table.Columns.Add("OrderInfoCode", typeof(string));
            table.Columns.Add("OrderProductName", typeof(string));
            table.Columns.Add("CtmName", typeof(string));
            return table;
        }
        /// <summary>
        /// 创建明细收费项目表
        /// 
        /// </summary>
        /// <returns></returns>
        private DataTable CreateShopBillSubDetail()
        {
            DataTable table = new DataTable();
            table.Columns.Add("BillDetailId", typeof(int));
            table.Columns.Add("DetailName", typeof(string));
            table.Columns.Add("Spec", typeof(string));
            table.Columns.Add("Num", typeof(int));
            table.Columns.Add("UnitPrice", typeof(double));
            table.Columns.Add("Count", typeof(double));
            table.Columns.Add("Area", typeof(double));
            return table;
        }
        #endregion
    }
}
